<template>
  <Row type="flex">
    <Col flex="600px"> <h2 class="title2">技术工人 <i>{{ techConfig.page.total }}</i></h2></Col>
    <Col flex="550px">
      <Row :gutter="[12, 8]">
        <Col :span="10">
          <Input v-model:value="techConfig.params.name" placeholder="请输入人员姓名" />
        </Col>
        <Col :span="10">
          <Select v-model:value="techConfig.params.type">
            <Option value="">全部技能类别</Option>
            <Option :value="item.id" v-for="item in dictionary.type">{{ item.dataValue }}</Option>
          </Select>
        </Col>
        <Col :span="4" class="text-right">
          <Button type="primary" @click="techConfig.handleSearch">查询</Button>
        </Col>
      </Row>
    </Col>
  </Row>

  <Table
      :columns="techConfig.columns"
      :row-key="record => record.id"
      :data-source="techConfig.list"
      :pagination="techConfig.page"
      :loading="techConfig.loading"
      @change="(pagination)=> changeHandler(pagination, techConfig)"
      bordered
  >
    <template #indexSlot="{ index }">{{(techConfig.page.current-1)*10+index+1}}</template>
    <template #name="{ text, record }">
      <router-link target="_blank" :to="{ path: `/staff/${record.personId}` }" class="blue" >
        {{ text }}
      </router-link>
    </template>
  </Table>
</template>

<script lang="ts">
import {defineComponent, inject, onMounted, reactive, ref, toRefs} from 'vue';
import {Row, Col, Table, Input, Select, Button, Modal, Descriptions,} from 'ant-design-vue';
import { getCompanyStaffApi , getIndexCodeApi } from '/@/api/company';
import { parseTime } from '/@/utils';

// 技术人员
const techColumns = [
  { title: '序号', dataIndex: 'index', width: 70, align: 'center', slots: ({ customRender: 'indexSlot' }) },
  { title: '姓名', dataIndex: 'personName', width: 85, slots: ({ customRender: 'name' }) },
  { title: '身份证号', dataIndex: 'cardNo' },
  { title: '证书编号', dataIndex: 'certNo' },
  { title: '资格类别', dataIndex: 'qualificationName' },
  { title: '发证日期', dataIndex: 'releaseDate', width: 120, customRender: ({ record })=> parseTime(record.releaseDate) },
  { title: '资格有效期至', dataIndex: 'validEdate', width: 120, customRender: ({ record })=> parseTime(record.validEdate) },
  { title: '发证机关', dataIndex: 'organName' , width: 300}
]

export default defineComponent({
  components: {
    Table , Row, Col, Input, Button, Select, Option: Select.Option,
    Modal,Descriptions,DescriptionsItem:Descriptions.Item,
  },
  setup() {
    const corpId = inject('corpId') as string;
    const visible = ref<boolean>(false);
    const recordDetail = ref<any>()
    const dictionary = reactive({
      type:[]
    })

    const techConfig = reactive({
      key: 'tech',
      columns: techColumns,
      list: [],
      loading: false,
      page: {
        total: 0,
        current: 1
      },
      params:{
        type:'',
        name:''
      },
      handleSearch:()=>{
        techConfig.loading = true;
        let { name  , type } = techConfig.params;
        getCompanyStaffApi('tech', {
          corpId, current:1, name  , type
        }).then(({ data, success }) => {
          if(success) {
            let { records, current, total } = data;
            techConfig.list = records;
            techConfig.loading = false;
            techConfig.page = {current, total};
          }else {
            techConfig.loading = false;
          }
        })
      }
    })


    const openDetail = data =>{
      recordDetail.value = data
      visible.value = true;
    }

    const changeHandler = (pagination, config) => {
      const { current } = pagination;
      let { loading, list, page } = toRefs(config);
      loading.value = true;
      getCompanyStaffApi(config.key, { corpId, current }).then(({ data, success }) => {
        if(success) {
          let { records, current, total } = data;
          list.value = records;
          loading.value = false;
          page.value = {current, total};
        }
      })
    }

    const getIndexCode = (data,back) =>{
      getIndexCodeApi(data).then(res=>{
        if(back)back(res)
      })
    }

    const init = () => {
      changeHandler({current: 1}, techConfig);

      getIndexCode('RY_TECH_TYPE',function (res){   //初始化 技术工人类别 字典表
        dictionary.type = res;
      })
    }

    onMounted(init)

    return {
      techConfig, changeHandler , dictionary ,visible ,recordDetail , parseTime , openDetail
    }
  },
})
</script>

<style lang="less" scoped>

.ant-col{
  padding-top: 16px;
  margin-bottom: 8px;
}

.ant-select ,.ant-btn{
  width: 100%;
}

</style>
